package org.easy.tencim;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: Li chaojie
 * @Date: 2022/3/21
 * @Description: https://cloud.tencent.com/document/product/269/1520
 */
@Configuration
@EnableConfigurationProperties({IMProperties.class})
@AutoConfigureAfter
@Slf4j
@ConditionalOnProperty(prefix = IMProperties.PREFIX, name = "enabled", havingValue = "true")
public class IMAutoConfiguration implements InitializingBean {

    private final IMProperties imProperties;

    public IMAutoConfiguration(IMProperties imProperties) {
        this.imProperties = imProperties;
    }

    @Bean
    @ConditionalOnMissingBean
    public TencentImTemplate tencentImTemplate() {
        return new TencentImTemplate(imProperties.getAppid(), imProperties.getKey(), imProperties.getExpire());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        checkParam();
    }

    private void checkParam() {
        if (imProperties.getAppid() == null ||
                imProperties.getKey() == null ||
                imProperties.getExpire() == null ||
                imProperties.getAdmin() == null) {
            log.error("未配置" + IMProperties.class.getTypeName());
        }
    }

    public static void main(String[] args) {
        TencentImTemplate tencentImTemplate = new TencentImTemplate(1600055743L, "39dc8f2f448751816883744f5db35612bcfaa7ea13a5dd38cfc1e1c72fad8e37", 16000);
        tencentImTemplate.accountDelete("1168267653287936");
    }
}
